package com.itbomb.tvdemo;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.NonNull;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Util;

import java.nio.ByteBuffer;
import java.security.MessageDigest;


/**
 * @author A.D.Wilme
 * @date on 2018/8/28  17:03
 * @email A.D.Wilme@gmail.com
 * @describe 圆角倒影
 */
public class ReflectedRoundedCorners extends BitmapTransformation {
    private static final String ID = "com.bumptech.glide.load.resource.bitmap.RoundedCorners";
    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    private int roundingRadius;
    private int color;

    /**
     * @param roundingRadius the corner radius (in device-specific pixels).
     * @param color          parent's background color
     * @throws IllegalArgumentException if rounding radius is 0 or less.
     */
    public ReflectedRoundedCorners(int roundingRadius, int color) {
        Preconditions.checkArgument(roundingRadius > 0, "roundingRadius must be greater than 0.");
        this.roundingRadius = roundingRadius;
        this.color = color;
    }

    @Override
    protected Bitmap transform(
            @NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap roundedCornersBitmap = TransformationUtils.roundedCorners(pool, toTransform, roundingRadius);
        return createReflectedRoundedCornersBitmap(roundedCornersBitmap, roundingRadius, color);
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof RoundedCorners) {
            ReflectedRoundedCorners other = (ReflectedRoundedCorners) o;
            return roundingRadius == other.roundingRadius;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Util.hashCode(ID.hashCode(),
                Util.hashCode(roundingRadius));
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);

        byte[] radiusData = ByteBuffer.allocate(4).putInt(roundingRadius).array();
        messageDigest.update(radiusData);
    }

    // 获得带倒影的图片
    private Bitmap createReflectedRoundedCornersBitmap(Bitmap bitmap, float roundingRadius, int color) {
        if (bitmap == null) {
            return null;
        }
        //倒影和图片本身的间距
        final int reflectedGap = 10;
        //阴影边距
        final int shadowGap = 0;

        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        Bitmap reflectedImage = Bitmap.createBitmap(bitmap, 0, height / 4 * 3,
                width, height / 4, matrix, false);
        Bitmap reflectedBitmap = Bitmap.createBitmap(width,
                (height + height / 4), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(reflectedBitmap);

        //绘制源图像
        Paint sourcePaint = new Paint();
        RectF shadowRectF = new RectF(shadowGap, shadowGap, width - shadowGap * 2, height - shadowGap * 2);
        canvas.drawBitmap(bitmap, null, shadowRectF, sourcePaint);


        //绘制间隙
        Paint defaultPaint = new Paint();
        defaultPaint.setColor(0);
        RectF rectF = new RectF(0, height, width, height + reflectedGap);
        canvas.drawRect(rectF, defaultPaint);

        //绘制复制图片背景
        Paint reflectedBackgroundPaint = new Paint();
        reflectedBackgroundPaint.setColor(color);
        RectF backgroundRectF = new RectF(0, height + reflectedGap, width, reflectedBitmap.getHeight() + reflectedGap);
        canvas.drawRoundRect(backgroundRectF, roundingRadius, roundingRadius, reflectedBackgroundPaint);

        //绘制复制图片
        Paint reflectedPaint = new Paint();
        canvas.drawBitmap(reflectedImage, 0, height + reflectedGap, reflectedPaint);

        //绘制渐变效果
        Paint paint = new Paint();
        int[] rgb = colorIntToARGB(color);
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight() + reflectedGap, 0,
                reflectedBitmap.getHeight() + reflectedGap, Color.argb(128, rgb[0], rgb[1], rgb[2]),
                Color.argb(255, rgb[0], rgb[1], rgb[2]), Shader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        canvas.drawRect(0, height, width, reflectedBitmap.getHeight()
                + reflectedGap, paint);
        return reflectedBitmap;
    }


    private int[] colorIntToARGB(int color) {
        int[] rgb = new int[3];
        rgb[0] = (color & 0xff0000) >> 16;
        rgb[1] = (color & 0x00ff00) >> 8;
        rgb[2] = (color & 0x0000ff);
        return rgb;
    }
}

